package top.zhangjianyong.blog.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import top.zhangjianyong.blog.utils.FileUtils;
import java.util.Objects;

/**
 * 文件扩展名枚举类
 * 包含文件扩展名和对应的contentType
 *
 * @author zhangjianyong
 * @since 2025/1/27
 */
@AllArgsConstructor
@Getter
public enum FileExtension {

    // 图片格式
    JPG("jpg", "image/jpeg"),
    JPEG("jpeg", "image/jpeg"),
    PNG("png", "image/png"),
    GIF("gif", "image/gif"),
    BMP("bmp", "image/bmp"),
    WEBP("webp", "image/webp"),
    SVG("svg", "image/svg+xml"),
    ICO("ico", "image/x-icon"),

    // 文档格式
    PDF("pdf", "application/pdf"),
    DOC("doc", "application/msword"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    XLS("xls", "application/vnd.ms-excel"),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    PPT("ppt", "application/vnd.ms-powerpoint"),
    PPTX("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),
    TXT("txt", "text/plain"),
    RTF("rtf", "application/rtf"),

    // 压缩文件
    ZIP("zip", "application/zip"),
    RAR("rar", "application/vnd.rar"),
    TAR("tar", "application/x-tar"),
    GZ("gz", "application/gzip"),
    SEVEN_Z("7z", "application/x-7z-compressed"),

    // 音频格式
    MP3("mp3", "audio/mpeg"),
    WAV("wav", "audio/wav"),
    FLAC("flac", "audio/flac"),
    AAC("aac", "audio/aac"),
    OGG("ogg", "audio/ogg"),

    // 视频格式
    MP4("mp4", "video/mp4"),
    AVI("avi", "video/x-msvideo"),
    MOV("mov", "video/quicktime"),
    WMV("wmv", "video/x-ms-wmv"),
    FLV("flv", "video/x-flv"),
    WEBM("webm", "video/webm"),

    // 代码文件
    JS("js", "application/javascript"),
    CSS("css", "text/css"),
    HTML("html", "text/html"),
    HTM("htm", "text/html"),
    XML("xml", "application/xml"),
    JSON("json", "application/json"),

    // 其他格式
    CSV("csv", "text/csv"),
    LOG("log", "text/plain"),
    MD("md", "text/markdown"),
    MARKDOWN("markdown", "text/markdown");

    private final String extension;
    private final String contentType;

    /**
     * 根据文件扩展名获取枚举值
     *
     * @param extension 文件扩展名（不包含点号）
     * @return 对应的枚举值，如果不存在则返回null
     */
    public static FileExtension getByExtension(String extension) {
        if (extension == null) return null;

        String ext = extension.toLowerCase().trim();
        for (FileExtension value : FileExtension.values()) {
            if (Objects.equals(value.getExtension(), ext)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 根据文件名获取对应的contentType
     *
     * @param fileName 文件名
     * @return contentType，如果无法识别则返回默认的application/octet-stream
     */
    public static String getContentTypeByFileName(String fileName) {
        if (fileName == null || fileName.trim().isEmpty()) {
            return "application/octet-stream";
        }

        String fileNameLower = fileName.toLowerCase();
        int lastDotIndex = fileNameLower.lastIndexOf('.');

        if (lastDotIndex == -1 || lastDotIndex == fileNameLower.length() - 1) {
            return "application/octet-stream";
        }

        String extension = FileUtils.getExtension(fileName);
        return getContentTypeByExtension(extension);
    }


    /**
     * 根据文件扩展名获取contentType
     *
     * @param extension 文件扩展名
     * @return contentType
     */
    public static String getContentTypeByExtension(String extension) {
        FileExtension fileExtension = getByExtension(extension);
        return fileExtension != null ? fileExtension.getContentType() : "application/octet-stream";
    }

    /**
     * 检查是否为图片文件
     *
     * @param fileName 文件名
     * @return 是否为图片文件
     */
    public static boolean isImageFile(String fileName) {
        String contentType = getContentTypeByFileName(fileName);
        return contentType.startsWith("image/");
    }

    /**
     * 检查是否为视频文件
     *
     * @param fileName 文件名
     * @return 是否为视频文件
     */
    public static boolean isVideoFile(String fileName) {
        String contentType = getContentTypeByFileName(fileName);
        return contentType.startsWith("video/");
    }

    /**
     * 检查是否为音频文件
     *
     * @param fileName 文件名
     * @return 是否为音频文件
     */
    public static boolean isAudioFile(String fileName) {
        String contentType = getContentTypeByFileName(fileName);
        return contentType.startsWith("audio/");
    }

    /**
     * 检查是否为文档文件
     *
     * @param fileName 文件名
     * @return 是否为文档文件
     */
    public static boolean isDocumentFile(String fileName) {
        String contentType = getContentTypeByFileName(fileName);
        return contentType.equals("application/pdf") ||
            contentType.equals("application/msword") ||
            contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||
            contentType.equals("application/vnd.ms-excel") ||
            contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ||
            contentType.equals("application/vnd.ms-powerpoint") ||
            contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation") ||
            contentType.equals("text/plain") ||
            contentType.equals("application/rtf");
    }
} 